#!/usr/bin/env python
from math import cos, sin
from Engine.math_ext import *
from Engine.Const import SCREENWIDTH, SCREENHEIGHT
from pygame import *
from Engine.Physics_object import Physics_object

class Bullet(Physics_object):
    def __init__(self, level, x, y, image, speed, direction, dammage, trail):
        image = transform.rotate(image, direction)
        self.init(level, x, y, image)
        self.speed = speed
        self.direction = direction
        self.gravity = 0
        self.dammage = dammage
        self.trail = trail
        self.created = 1
        self.hspeed = cos(self.direction) * self.speed
        self.vspeed = -sin(self.direction) * self.speed
        self.coll_only_solid = False
        if trail != None:
            pass
        
    def override_update(self, level, events):
        cam_x = level.camera.get_x()
        cam_y = level.camera.get_y()
        if (self.rect[0] - cam_x > SCREENWIDTH or self.rect[0] - cam_x + self.rect[2] < 0 or
            self.rect[1] - cam_y > SCREENHEIGHT or self.rect[1] - cam_y + self.rect[3] < 0):
            level.remove(self, self.region)

        if self.trail != None:
            self.trail.update()
    
    def collision_other(self, other):
        other.hit(self.dammage, (self.rect[0], self.rect[1]))
        self.event_death()
        self.level.remove(self, self.region)
        
    def collision_self(self, other):
        other.hit(self.dammage, (self.rect[0], self.rect[1]))
        self.event_death()
        self.level.remove(self, self.region)
    
    def event_death(self):
        pass
    
    def override_front_draw(self, camera):
        if self.trail != None:
            self.trail.draw(camera, (self.x, self.y))